package com.wcfsy.wechat.wx.um.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wcfsy.wechat.wx.exception.ServiceException;
import com.wcfsy.wechat.wx.um.dao.IMenuDao;
import com.wcfsy.wechat.wx.um.entity.Menu;
import com.wcfsy.wechat.wx.um.service.IMenuService;

@Service("menuService")
public class MenuServiceImpl implements IMenuService {

	@Resource
	private IMenuDao menuDao;

	public Menu queryMenu(String paramString) throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Menu> queryAllMenu() throws ServiceException {
		return menuDao.findByParam(new HashMap<String, Object>());
	}

	public List<Menu> findMenuTree() {
		List<Menu> list = queryMenuByParentId(0L);
		for (Menu menu : list) {
			createMenu(menu);
		}
		return list;
	}

	private void createMenu(Menu parentNode) {
		List<Menu> subMenuList = queryMenuByParentId(parentNode.getId());
		parentNode.setSubMenuList(subMenuList);
	}

	public List<Menu> queryTotalMenu() throws ServiceException {
		return menuDao.findByParam(new HashMap<String, Object>());
	}

	public void updateMenu(Menu menu) throws ServiceException {
		try {
			menuDao.update(menu);
		} catch (Exception e) {
			throw new ServiceException("001", "更新菜单失败");
		}

	}

	public Menu getMenuById(long id) throws ServiceException {
		return menuDao.getById(id);
	}

	public Long addMenu(Menu menu) throws ServiceException {
		try {
			return menuDao.add(menu);
		} catch (Exception e) {
			throw new ServiceException("001", "新增菜单失败");
		}
	}

	public void deleteMenuById(long id) throws ServiceException {
		try {
			menuDao.deleteById(id);
		} catch (Exception e) {
			throw new ServiceException("001", "删除菜单失败");
		}
	}

	public List<Menu> queryMenuByParam(Menu paramMenu) throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Menu> queryMenuByParentId(Long parentId)
			throws ServiceException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentId", Long.valueOf(parentId));
		return menuDao.findByParam(map);
	}

	public void batchInsertMenu(List<Menu> paramList) throws ServiceException {
		// TODO Auto-generated method stub

	}

}
